/*
Copyright (c) 2010 julien barbay <barbay.julien@gmail.com>

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the "Software"), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
*/

//TODO : Think about improvements
package martian.m4gic.services
{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	
	import martian.m4gic.display.Smartie;
	
	public class GUI extends Service
	{
		public function GUI() { super(); }
		
		/* service name */
		static private const NAME:String = "GUI";
		
		/* service properties */
		static public function get length():int { return layers.length; }
		static private var layers:Vector.<Smartie>;
		static private var labels:Dictionary;

		static private var center:Point = new Point();
			static public function get middle():Point
			{
				center.x = stage.stageWidth / 2;
				center.y = stage.stageHeight / 2;
				
				return center.clone();
			}
			
		static private var rect:Rectangle = new Rectangle();
			static public function get screen():Rectangle
			{
				screen.width = stage.stageWidth;
				screen.height = stage.stageHeight;
				
				return screen.clone();
			}
		
		/* service initialization */
		static private function _register(args:Object = null):void 
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			var sprites:int = args.layers != undefined ? args.layers : 3;
			
			layers = new Vector.<Smartie>();
			for (var i:int = 0; i < sprites; i++) { layers.push(reference.addChild(new Smartie()) as Smartie); }
				
			labels = new Dictionary();
		}
		
		static private function _release(args:Object = null):void 
		{
			labels = null;	
				
				for (var i:int = 0; i < layers.length; i++)
				{ layers[i] = null; }
				
			layers = null;
		}
		
		static private function _activate(args:Object = null):void {}
		static private function _desactivate(args:Object = null):void { }
		
		/* service methods */
		static public function layer(arg:*):Smartie
		{
			if (available)
			{
				var index:int;
				if (arg is int) { index = arg; }
				else if (arg is String) { index = labels[arg]; }
				
				return layers[index];
			}
			
			return null;
		}
		
		static public function label(layer:int, name:String):void { if (available) { labels[name] = layer; } }	
		
		/* inheritance */
		static private var reference:Sprite;
		static private var dispatch:EventDispatcher;
		static private var stage:Stage;
		
		static private var registered:Boolean = false;
		static private var activated:Boolean = false;
			static public function get available():Boolean { return (registered && activated); }
		
		static public function register(container:Sprite, args:Object = null):void
		{
			if (!registered)
			{
				reference = container;
				dispatch = new EventDispatcher();
				stage = reference.stage;
				
				_register(args);
				
				registered = true;
				
				if (args != null) { if (args.active != undefined) { if (args.active == true) { activate(); } } }
			}
			else { trace("6:", NAME, "already registered."); }
		}
		
		static public function release(args:Object = null):void
		{
			if (registered && !activated)
			{
				stage = null;
				dispatch = null;
				reference = null;
				
				_release(args);
				
				registered = false;
			}
			else { trace("6:", NAME, "already released."); }
		}
		
		static public function activate(args:Object = null):void
		{
			if (registered && !activated)
			{
				activated = true;
				_activate(args);
			}			
		}
		
		static public function desactivate(args:Object = null):void
		{
			if (registered && activated)
			{
				activated = false;
				_desactivate(args);
			}
		}
		
		static private function dispatchEvent(event:Event):void
		{
			dispatch.dispatchEvent(event);
		}
		
		static public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
		{
			dispatch.addEventListener(type, listener, useCapture, priority, useWeakReference);
		}
		
		static public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
		{
			dispatch.removeEventListener(type, listener, useCapture);
		}
	}
}